package hp.com.service.impl;

import hp.com.dao.ProductDAO;
import hp.com.dao.impl.ProductDAOImpl;
import hp.com.entity.Sells;
import hp.com.service.IndexService;
import hp.com.util.GeneralUtil;

import java.util.List;
import java.util.Random;

public class IndexServiceImpl implements IndexService {
    ProductDAO dao = new ProductDAOImpl();
    Random random = new Random();

    @Override
    public List<Sells> getSellsMain() throws Exception {
        int[] ids = new int[5];
        //查询一共有多少商品
        int count = dao.getProductCount();
        //获取随机的5位数来取得商品
        for (int i = 0; i < 5; i++) {
            //获取一个随机数
            int id = random.nextInt(count+1);
            //检查随机数是否重复
            while (!GeneralUtil.checkRepetition(ids, id)) {
                id = random.nextInt(count+1);
            }
            ids[i] = id;
        }
        return dao.getSellById(ids);
    }
}